package com.example.lawyer;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;

import com.example.lawyer.community.PageHomeB;
import com.example.lawyer.databinding.ActivityHomeBinding;
import com.example.lawyer.netty.NettyStart;
import com.example.lawyer.service.NettyService;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class HomeActivity extends AppCompatActivity{


    private ActivityHomeBinding activityHomeBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityHomeBinding =  DataBindingUtil.setContentView(this, R.layout.activity_home);

        initlistener();
//        StartService();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        Intent intens = new Intent(getApplicationContext(),NettyService.class);
        getApplicationContext().stopService(intens);
    }

    private void initlistener() {
        final NavController navController = Navigation.findNavController(HomeActivity.this, R.id.fragment_home);
        activityHomeBinding.botonnavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                System.out.println(menuItem.getItemId());
                switch (menuItem.getItemId()) {
                    case R.id.home_btn_a:
                        navController.navigate(R.id.pageHomeA);
                        break;
                    case  R.id.home_btn_b:
                        navController.navigate(R.id.fragment_page_home_b);
                        break;
                    case  R.id.home_btn_c:
                        navController.navigate(R.id.fragment_page_home_c);
                        break;
                    case  R.id.home_btn_d:
                        navController.navigate(R.id.fragment_page_home_d);
                        break;
                }

                return true;
            }
        });
    }



    HandlerThread handlerThread =  new HandlerThread("notia");

    Handler handler  =   new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            Toast.makeText(getApplicationContext(),"收到消息",Toast.LENGTH_LONG).show();

        }
    };



    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i("DDDD","服务连接");
            NettyService.MyBindder ss = (NettyService.MyBindder) service;
            NettyService myBind = ss.getMyBind();
            myBind.SetOnSendActivtyListener(new NettyService.SendActivityMessage() {

                @Override
                public void ReceiveMsg(String str,String type) {
                    System.out.println("HomeActivity收到来自Netty的消息");
                    System.out.println(str);
                    Message obtain = Message.obtain();
                    obtain.what = 96527;
                    obtain.obj = str;
                    handler.sendMessage(obtain);
                }
            });
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i("DDDD","断开连接");
        }
    };
    private void StartService()
    {
        Intent intent = new Intent(this.getApplicationContext(), NettyService.class);
        this.getApplicationContext().bindService(intent,connection,BIND_AUTO_CREATE);
        startService(intent);
        System.out.println("启动完毕");
    }

}
